package system

import (
	"github.com/go-yaml/yaml"
	"io/ioutil"
)

type Configuration struct {
	DSN           string `yaml:"users"`     //database dsn
	PageSize      int    `yaml:"page_size"` //page_size
	MYSQL         string `yaml:"mysql"`
	SignupEnabled bool   `yaml:"signup_enabled"`
	SessionSecret string `yaml:"session_secret"`

	GithubClientId     string `yaml:"github_clientid"` // github
	GithubClientSecret string `yaml:"github_clientsecret"`
	GithubAuthUrl      string `yaml:"github_authurl"`
	GithubRedirectURL  string `yaml:"github_redirecturl"`
	GithubTokenUrl     string `yaml:"github_tokenurl"`
	GithubScope        string `yaml:"github_scope"`
}

const (
	DEFAULT_PAGESIZE = 10
)

var configuration *Configuration

func LoadConfiguration(path string) error {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	var config Configuration
	err = yaml.Unmarshal(data, &config)
	if err != nil {
		return err
	}
	if config.PageSize <= 0 {
		config.PageSize = DEFAULT_PAGESIZE
	}
	configuration = &config
	return err

}

func GetConfiguration() *Configuration {
	return configuration
}
